package com.cy;

import cn.hutool.core.io.FileUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class NovelCrawler {

    @Test
    public void test01() throws IOException {
        String url = "http://www.xbiquge.la/13/13959/";
        String title = parseTitle(url);
        System.out.println(title);
    }

    
    private String parseTitle(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        return doc.selectFirst("#info>h1").text();
    }

    @Test
    public void test02() throws IOException {
        String url = "http://www.xbiquge.la/13/13959/";
        List<String> categoryList = parseCategoryList(url);
        categoryList.forEach(System.out::println);
    }

    private List<String> parseCategoryList(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        Elements elements = doc.select("#list a");
        List<String> categoryList = new ArrayList<>();
        elements.forEach(a->categoryList.add(a.attr("abs:href")));
        return categoryList;
    }

    @Test
    public void test03() throws IOException {
        String url = "http://www.xbiquge.la/13/13959/5939025.html";
        String bookName = bookNameTitle(url);
        System.out.println(bookName);
    }

    private String bookNameTitle(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        return doc.selectFirst(".bookname>h1").text();
    }

    @Test
    public void test04() throws IOException {
        String url = "http://www.xbiquge.la/13/13959/5939025.html";
        String content = parseContent(url);
        System.out.println(content);
    }

    private String parseContent(String url) throws IOException {
        Document doc = Jsoup.connect(url).get();
        Element element = doc.selectFirst("#content");
        element.select("p").remove();
        String content = element.html();
        content = content.replace("&nbsp;", " ")
                         .replace("<br>", "");
        return content;
    }

    @Test
    public void test05() throws IOException {
        String url = "http://www.xbiquge.la/13/13959/";
        downloadOne(url);
    }

    private void downloadOne(String url) {
        BufferedWriter bw = null;
        try {
            //1.解析小说的标题
            String title = parseTitle(url);
            bw = FileUtil.getWriter(title + ".txt", "UTF-8",false);
            //2.解析小说的目录
            List<String> categoryList = parseCategoryList(url);
            //3.遍历目录
            for (int i = 0;i<categoryList.size();){
                String aUrl = categoryList.get(i);
                try {
                    //解析章节的标题
                    String bookName = bookNameTitle(aUrl);
                    System.err.println("正在下载["+bookName+"]...");
                    //解析章节的内容
                    String content = parseContent(aUrl);
                    //保存到文件中
                    bw.write(bookName);
                    bw.newLine();
                    bw.newLine();
                    bw.write(content);
                    System.out.println("["+bookName+"]下载完成！！！");
                    i++;
                    //Thread.sleep(1000);
                } catch (IOException e) {
                    System.err.println("下载["+aUrl+"]失败，5秒后重新下载...");
                    Thread.sleep(5000);
                }
                /*if (i == 1000){
                    break;
                }*/
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
